package com.ray.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.ObjectIdGenerators.StringIdGenerator;
import com.ray.bean.CustomerBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DepartmentBean;
import com.ray.bean.FeedOrderDetailBean;
import com.ray.bean.FileManagerBean;
import com.ray.bean.MaterialBean;
import com.ray.bean.OrderInfoTemplateBean;
import com.ray.bean.PersonBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.OrderCreateMapper;
import com.ray.pojo.MyUser;
import com.ray.service.BasicDataService;
import com.ray.service.FileManagerService;
import com.ray.service.OrderCreateService;
import com.ray.service.OrderInfoTemplateService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.UploadFilesUtil;
import com.ray.util.qiniu.QiniuConfig;
import com.ray.util.qiniu.QiniuUploadUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 文件管理
 * 订单创建数据获取及提交
 * @author Shensg
 * 2018年1月24日
 */

@SuppressWarnings("unused")
@RestController
@RequestMapping("/fileManager")
public class FileManagerController {
	
	@Autowired
	FileManagerService fileManagerService;
	
	//获取七牛token
	@RequestMapping("/getQiniuToken")
	public Map getQiniuToken() throws IOException, ServiceException  {  
		return ResponseUtil.ResponseResult(QiniuUploadUtils.getToken(QiniuConfig.accessKey, QiniuConfig.secretKey, QiniuConfig.bucketShinehaopic));
	}
	
	//文件上传
	@RequestMapping("/uploadFile")
	public Map uploadFile(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		List list = UploadFilesUtil.loadFileByQiniu4BucketShinehaopic(files,request);
		FileManagerBean fileManager = null;
		for (int i = 0; i < list.size(); i++) {
			fileManager=new FileManagerBean(0, map.get("order_no").toString(), map.get("type").toString(), "", map.get("loginUserName").toString(), map.get("loginCompany").toString(), files[i].getOriginalFilename(), list.get(i).toString(), Integer.parseInt(map.get("type").toString()));
			fileManagerService.saveFileManager(fileManager);
		}
		return ResponseUtil.ResponseResult(list);
	} 
	//文件上传
	@RequestMapping("/uploadFileByBase64")
	public Map uploadFileByBase64(HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		List list = UploadFilesUtil.loadBase64FileByQiniu4BucketShinehaopic(map.get("base64content").toString(), map.get("order_no").toString()+"-base64Img",request);
		FileManagerBean fileManager = null;
		for (int i = 0; i < list.size(); i++) {
			fileManager=new FileManagerBean(0, map.get("order_no").toString(), map.get("type").toString(), "", map.get("loginUserName").toString(), map.get("loginCompany").toString(), map.get("order_no").toString()+"-base64Img", list.get(i).toString(), 1);
			fileManagerService.saveFileManager(fileManager);
			System.out.println(fileManager.toString());
		}
		return ResponseUtil.ResponseResult(list);
	} 
	//文件列表
	@RequestMapping("/getFileList")
	public Map getFileList(HttpServletRequest request,FileManagerBean fileManager) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		fileManager.setCompany(map.get("loginCompany").toString());
		return ResponseUtil.ResponseResult(fileManagerService.getFileList(fileManager));
	}
	
	//文件列表
	@RequestMapping("/getFileListForPage")
	public DatatablesViewPageBean<FileManagerBean> getFileListForPage(HttpServletRequest request,FileManagerBean fileManager) throws IOException, ServiceException  {  
		DatatablesViewPageBean<FileManagerBean> retBean = new DatatablesViewPageBean<FileManagerBean>();
		Map map=RequestUtil.getRequestAllParam(request);
		fileManager.setCompany(map.get("loginCompany").toString());
		retBean.setReturnData(fileManagerService.getFileList(fileManager));
		int count = fileManagerService.getFileListCount(fileManager);
		retBean.setiTotalDisplayRecords(count); 
		retBean.setiTotalRecords(count);
		return retBean;
	}
	
	//文件上传ForPrint
	@RequestMapping("/uploadFileForPrint")
	public Map uploadFileForPrint(HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		FileManagerBean fileManager = null;
		String[] urls =  map.get("url").toString().split(",");
		for (String url : urls) {
			fileManager=new FileManagerBean(0, map.get("order_no").toString(), map.get("type").toString(), "", map.get("loginUserName").toString(), map.get("loginCompany").toString(), map.get("file_name").toString(),url, 1);
			fileManagerService.saveFileManager(fileManager);
		}
		System.out.println(fileManager.toString());
		return ResponseUtil.ResponseResult("保存成功");
	} 
	@RequestMapping("/getSerialNoForPrint")
	public Map getSerialNoForPrint(HttpServletRequest request) throws IOException, ServiceException  {  
		String ptNo = fileManagerService.getSerialNo("PT");
		return ResponseUtil.ResponseResult(ptNo);
	}
	@RequestMapping("/deleteImgByOrderIdAndType")
	public Map deleteImgByOrderIdAndType(HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		fileManagerService.deleteImgByOrderIdAndType(map);
		return ResponseUtil.ResponseResult("删除成功");
	}
}
